package Client;
import javax.swing.*;


import Entities.Candidate;
import Entities.Voter;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

//public class mainPage implements ActionListener{
//	JFrame Nframe;
//	Boardb mpanel;
//	Board board;
//	public mainPage(Voter v, Board board){
//		this.board = board;
//		Nframe = new JFrame("Online Voting System!");
//		Nframe.setSize(600,600);
//		Nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		Nframe.setResizable(true);
//		try {
//			mpanel = new Boardb(v);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		Nframe.setContentPane(mpanel);	
//
//		JButton back = new JButton("Back");
//		back.setBounds(60, 350, 150, 30);
//		back.addActionListener(this);
//		back.setActionCommand("back");
//		mpanel.add(back);
//		back.setVisible(true);
//		Nframe.setVisible(false);
//	}
//
//	public void setV(){
//		Nframe.setVisible(true);
//	}
//
//	public void actionPerformed(ActionEvent e){
//		if(e.getActionCommand().equals("back")){
//			this.setV2();
//			board.setVisible(true);
//		}
//	}
//
//	private void setV2() {
//		Nframe.setVisible(false);
//	}
//}
@SuppressWarnings("serial")
class Boardb extends JPanel implements ActionListener{	
	JLabel voteLabel, titleVote, confirmation;
	String[] candidateList;
	JPanel panelVertical;
	JRadioButton[] Candidates;
	JButton vote, trace;
	ButtonGroup btngrpVertical;
	ArrayList<Candidate> cds;
	int i;
	Voter vo;
	JFrame mframe;
	JButton back = new JButton("Logout");
	public Boardb(Voter vo,JFrame mframe) {
		back.setBounds(40, 350, 85, 30);
		back.addActionListener(this);
		back.setActionCommand("back");
		add(back);
		this.mframe = mframe;
		this.vo = vo;
		setLayout(null); 	
		setBackground(Color.WHITE);	
		
		titleVote = new JLabel("Voting");
		confirmation = new JLabel("You have just voted!");
		trace = new JButton("Trace your vote");
		voteLabel = new JLabel("Please vote for one of the following candidates:");
		vote = new JButton("Vote!");
		
		titleVote.setBounds(200,20,400,30);		// x/y/width/length
		voteLabel.setBounds(40,80,400,30);
		vote.setBounds(300, 350, 100, 30);
		trace.setBounds(420, 350, 130, 30);
		confirmation.setBounds(325,400,200,30);
		
		titleVote.setFont(new Font("Dialog", Font.PLAIN, 24));
		
		VotingClient client = new VotingClient();
		String incoming = "";
		try {
			incoming = client.sendMessage("candCanVote#"+vo.getSemster()+"#"+vo.getId());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int nofCandidates = 0;
		cds = new ArrayList<Candidate>();
		System.out.println(incoming + " karim");
		if(!incoming.equals(""))
		{
			String incomingSp [] = incoming.split("#");
			nofCandidates = Integer.parseInt(incomingSp[0]);
			for(int i=0;i<nofCandidates;i++)
			{
				String candidate = incomingSp[i+1];
				String candidateSp[] = candidate.split("%");
				cds.add(new Candidate(Integer.parseInt(candidateSp[0]), Integer.parseInt(candidateSp[1]), candidateSp[2], candidateSp[3], candidateSp[4]));
			}
		}
		candidateList = new String[nofCandidates];
		for(i=0;i<candidateList.length;i++)
		{
			candidateList[i] = cds.get(i).getName();
		}
		panelVertical = new JPanel();
		panelVertical.setBounds(50, 120, 180, 200);
		panelVertical.setLayout(new BoxLayout(panelVertical, BoxLayout.Y_AXIS));
		Candidates = new JRadioButton[nofCandidates];
		btngrpVertical = new ButtonGroup();
		for (i = 0; i < Candidates.length; i++){  
			Candidates[i] = new JRadioButton(candidateList[i]);
			Candidates[i].addActionListener(this);
			Candidates[i].setActionCommand("candidate"+i);
			btngrpVertical.add(Candidates[i]);
			panelVertical.add(Candidates[i]);  
		}  
		panelVertical.setBorder(BorderFactory.createTitledBorder("Candidates"));

		vote.addActionListener(this);
		vote.setActionCommand("vote");	
		trace.addActionListener(this);
		trace.setActionCommand("tracevote");
		int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
		JScrollPane scroll = new JScrollPane(panelVertical,v,h);
		scroll.setBounds(50, 120, 180, 200);;
		scroll.getVerticalScrollBar().setValue(0);
		add(scroll,BorderLayout.PAGE_START);
		add(titleVote);
		add(voteLabel);
		add(vote);
		add(trace);	
		add(confirmation);
		
		titleVote.setVisible(true);
		confirmation.setVisible(false);
		vote.setVisible(true);
		trace.setVisible(true);
		voteLabel.setVisible(true);		
	}
	
	public void actionPerformed(ActionEvent e){	
		if(e.getActionCommand().equals("vote")){
			int i = -1;
			Enumeration<?> elements = btngrpVertical.getElements();
			boolean isFound = false;
			while(elements.hasMoreElements())
			{
				JRadioButton radio = (JRadioButton) elements.nextElement();
				i++;
				if(radio.isSelected())
				{
					isFound = true;
					break;
				}
			}
			if(i==-1||!isFound)
			{
				System.out.println("Doesnot Exists");
			}
			else
			{
				Candidate c = cds.get(i);
				//new confirmVote(c,vo,this).setV();
				mframe.getContentPane().removeAll();
				mframe.repaint();
				JPanel p = new Boardcv(c,vo,mframe);
				p.setBounds(0, 0, 600, 600);
				mframe.add(p);//.setV3();
				mframe.validate();
				mframe.repaint();
				System.out.println("go to next view");
			}
			
		}
		else if(e.getActionCommand().equals("back")){
			mframe.getContentPane().removeAll();
			mframe.repaint();
			JPanel p = new Board(mframe);
			p.setBounds(0, 0, 600, 600);
			mframe.add(p);//.setV3();
			mframe.validate();
			mframe.repaint();
		}
	}
}

